﻿using STT.Admin;
using STT.Business;
using STT.Entities;
using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Profile.UserControlsGV
{
    public partial class UcGuiTaiLieuHocVien : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            Loadddlophoc();
            LoadDSTaiLieu();
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void btnguitailieu_Click(object sender, EventArgs e)
        {
            if (FileTaiLieu.HasFile)
            {
                try
                {
                    string id = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString() + "¿" + FileTaiLieu.FileName;
                    FileTaiLieu.SaveAs(Server.MapPath("~/Uploads/Files/") + id);
                    ETbTaiLieu TaiLieu = new ETbTaiLieu();
                    TaiLieu.MaGV = Session["MaFGV"].ToString();
                    TaiLieu.MaLop = int.Parse(ddllophoc.SelectedValue.ToString());
                    TaiLieu.TenTL = txttenfile.Text;
                    TaiLieu.Link = "~/Uploads/Files/" + id;
                    TaiLieu.NgayGui = DateTime.Now;
                    TaiLieu.TieuDeGui = txtndtl.Text;
                    BTbTaiLieu.Insert(TaiLieu);
                    Huy();
                    LoadDSTaiLieu();
                    ThongBao("Bạn đã gửi thành công tài liệu");
                }
                catch { }
            }
            else
            {
                ThongBao("Bạn chưa chọn tài liệu để gửi");
            }
        }
        private void Loadddlophoc()
        {
            Methods.LopHocByMaGV(ref ddllophoc, Session["MaFGV"].ToString());
        }
        private void Huy()
        {
            txtndtl.Text = "";
            txttenfile.Text = "";
            Loadddlophoc();
        }
        private void LoadDSTaiLieu()
        {
            gvDSTaiLieu.DataSource = BTbTaiLieu.SelectAllByMaGV(Session["MaFGV"].ToString());
            gvDSTaiLieu.DataBind();
        }
        protected void gvDSTaiLieu_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvDSTaiLieu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDSTaiLieu.PageIndex = e.NewPageIndex;
            LoadDSTaiLieu();
        }
    }
}